package com.example.module_hp_zao_yin.utils;

import java.io.File;
import java.util.Locale;

public class HpFileUtil {
    public static String[] geShi = {"mp3","flac","wav","m4a"};
    public static String formatSize(long size) {
        String suffix = "B";
        double fSize = size;
        if (fSize > 1024) {
            suffix = "KB";
            fSize /= 1024;
        }
        if (fSize > 1024) {
            suffix = "MB";
            fSize /= 1024;
        }
        if (fSize > 1024) {
            suffix = "GB";
            fSize /= 1024;
        }
        return String.format(Locale.getDefault(), "%.2f %s", fSize, suffix);
    }
    public static String getDirOrFileSize(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return "";
        }
        if (file.isFile()) {
            return HpFileUtil.formatSize(file.length());
        } else {
            File[] files = file.listFiles();
            long size = 0;
            for (File f : files) {
                size += f.isDirectory() ? 0 : f.length();
            }
            return HpFileUtil.formatSize(size);
        }
    }
    public static String formatTime(int size){
        String  time;
        if(size<60){
            time=String.format("00:%02d",size%60);
        }else if(size<3600){
            time=String.format("%02d:%02d",size/60,size%60);
        }else{
            time=String.format("%02d:%02d:%02d",size/3600,size%3600/60,size%60);
        }
        return time;
    }
}
